package com.ktjiaoyu.crm.controller;
import com.ktjiaoyu.crm.pojo.User;
import com.ktjiaoyu.crm.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.Date;
import java.util.List;
@Controller
public class ExampleController {
    @Resource
    private UserService userService;

    //for循环
    @GetMapping(value = "/list")
    public String list(Model model){
        List<User> list=userService.findAllUsers();
        model.addAttribute("users",list);
        return "demo/list";
    }



    //条件判断if/unless
    @GetMapping(value = "/if")
    public String ifunless(Model model){
        model.addAttribute("flag","yes");
        return "demo/if";
    }


    @GetMapping(value = "/hello/{id}")
    public String getUser(@PathVariable("id") Long usrId, Model model){
        User user = userService.getUser(usrId);
        model.addAttribute("user",user);
        return "demo/hello";
    }
    //
    @GetMapping(value = "/string")
    public String getString(Model model, HttpServletRequest request){//演示赋值，字符串拼接
        model.addAttribute("userName","ktjiaoyu");
        request.setAttribute("test","request");
        request.getSession().setAttribute("test","session");
        model.addAttribute("date",new Date());
        return "demo/string";
    }
    //内联
    @GetMapping(value = "/inline")
    public String inline(Model model){
        model.addAttribute("userName","ktjiaoyu");
        return "demo/inline";
    }

}
